package com.echo.register.order.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.echo.register.common.result.Result;
import com.echo.register.common.util.AuthContextHolder;
import com.echo.register.enums.OrderStatusEnum;
import com.echo.register.model.order.OrderInfo;
import com.echo.register.order.service.OrderService;
import com.echo.register.vo.order.OrderCountQueryVo;
import com.echo.register.vo.order.OrderQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName: OrderApiController
 * @Package: com.echo.register.order.api
 * @Description: 订单接口
 * @Author 谢同学
 * @Create 2024/1/4 21:23
 * @Version 1.0
 */
@Api(tags = "订单接口")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {
    @Autowired
    private OrderService orderService;


    @ApiOperation(value = "创建订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result<Long> submitOrder(@PathVariable String scheduleId, @PathVariable Long patientId) {
        return Result.ok(orderService.saveOrder(scheduleId, patientId));
    }

    @ApiOperation(value = "订单列表（条件查询带分页）")
    @GetMapping("/auth/{page}/{limit}")
    public Result<IPage<OrderInfo>> selectPage(@PathVariable Long page,
                                @PathVariable Long limit,
                                OrderQueryVo orderQueryVo, HttpServletRequest request){
        // 获取订单id
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        Page<OrderInfo> pageParam = new Page<>(page, limit);
        return Result.ok(orderService.selectPage(pageParam, orderQueryVo));
    }
    @ApiOperation(value = "获取订单状态")
    @GetMapping("/auth/getStatusList")
    public Result<?> getStatusList() {
        return Result.ok(OrderStatusEnum.getStatusList());
    }

    @ApiOperation(value = "根据订单id查询订单详情")
    @GetMapping("/auth/getOrder/{orderId}")
    public Result<?> getOrderInfo(@PathVariable Long orderId) {

        return Result.ok(orderService.getOrderInfo(orderId));
    }

    @ApiOperation(value = "取消预约")
    @GetMapping("auth/cancelOrder/{orderId}")
    public Result<?> cancelOrder(@ApiParam(name = "orderId", value = "订单id", required = true)
                                     @PathVariable("orderId") Long orderId) {
        return Result.ok(orderService.cancelOrder(orderId));
    }

    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderService.getOrderCountMap(orderCountQueryVo);
    }
}
